package server.business.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import server.business.pojo.BloodSamplePatient;
import server.business.pojo.BloodSamplePurpose;
import server.business.service.BloodSamplePatientService;
import server.business.service.BloodSamplePurposeService;
import server.business.service.PatientService;
import server.business.utils.GenerateNumberBySnowFlake;
import server.business.utils.JsonResult;
import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/bloodSamplePatient")
public class BloodSamplePantientController {
    @Autowired
    private BloodSamplePatientService bloodSamplePatientService;
    @Autowired
    private PatientService patientService;


    /**
     * 添加血样信息
     * @param httpServletRequest
     * @return
     * @throws ParseException
     */
    @GetMapping("/normal/add")
    @ResponseBody
    public JsonResult<Object> bloodSampleAdd(HttpServletRequest httpServletRequest) throws ParseException {
        BloodSamplePatient bloodSamplePatient = new BloodSamplePatient();
        String medicalRecordNumber = httpServletRequest.getParameter("medicalRecordNumber");
        System.out.println(medicalRecordNumber);
        Long patientId=patientService.selectPatientId(medicalRecordNumber);
        System.out.println(patientId);
        bloodSamplePatient.setPatientId(patientId);


        bloodSamplePatient.setDrawPeople(httpServletRequest.getParameter("drawPeople"));
        DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");
        String drawTime=httpServletRequest.getParameter("drawTime");
        Date date = fmt.parse(drawTime);
        bloodSamplePatient.setDrawTime(date);
        bloodSamplePatient.setDrawPeopleNumber(httpServletRequest.getParameter("drawPeopleNumber"));

        long bloodSampleId = bloodSamplePatient.getPatientId();
        GenerateNumberBySnowFlake bloodSampleNumber1=new GenerateNumberBySnowFlake(bloodSampleId, "BSN");
        bloodSamplePatient.setBloodSampleNumber(bloodSampleNumber1.genNumberByflag());
        System.out.println(bloodSampleNumber1.genNumberByflag());
        bloodSamplePatient.setPurpose(Integer.valueOf(httpServletRequest.getParameter("purpose")));


        bloodSamplePatientService.saveOrUpdate(bloodSamplePatient);

        return JsonResult.success();
    }

    /**
     * 展示血样信息
     * @return
     */
    @RequestMapping("/normal/list")
    public JsonResult<Object> listPage() {
        Page<BloodSamplePatient> pageList =bloodSamplePatientService.getBloodSamplePatientPage();
        return JsonResult.success(pageList);
    }


    /**
     * 修改血样信息
     * @param bloodSamplePatient
     * @return
     */
    @PutMapping(value = "/normal/sampleupdate")
    public boolean save(@RequestBody BloodSamplePatient bloodSamplePatient) {
        return bloodSamplePatientService.updateById(bloodSamplePatient);

    }
    /**
     * 分页查询
     * */
    @GetMapping("/normal/{currentPage}/{pageSize}")
    public JsonResult<Object> getPage(@PathVariable int currentPage, @PathVariable int pageSize, BloodSamplePatient bloodSamplePatient, String startTime, String endTime){
        System.out.println(startTime);
        IPage<BloodSamplePatient> page =bloodSamplePatientService.getPage(currentPage,pageSize,bloodSamplePatient,startTime,endTime);

        if (currentPage>page.getPages()){
            page =bloodSamplePatientService.getPage((int)page.getPages(),pageSize,bloodSamplePatient,startTime,endTime);
        }
        return JsonResult.success(page);
    }


    /**
     * 批量删除
     */
    @RequestMapping("/normal/batchDelete")
    @ResponseBody
    public JsonResult<Object> batchDelete(@RequestBody ArrayList<Integer> ids) {
        System.out.println(ids);
        boolean success = bloodSamplePatientService.removeByIds(ids);
        if (success) {
            return JsonResult.success();
        } else {
            return JsonResult.error();
        }
    }


}
